/**
 * 
 */
package com.juliashine.db.zzy;

import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

import javax.sql.DataSource;



/**
 * @author Juliashine@gmail.com 2011-4-28
 *
 */
public class SimpleDataSource implements DataSource{
	
	
	DBDesc dbdesc;
	Properties properties;
	Connection conn;		
	
	public SimpleDataSource(){}
	
	public SimpleDataSource(DBDesc dbdesc){
		this.dbdesc = dbdesc;
		
	}
	
	public SimpleDataSource(Properties properties){
		this.properties = properties;
		if(dbdesc == null)
			dbdesc = new DBDesc();

		dbdesc.setDb(DBType.valueOf(properties.getProperty("dbtype")));
		dbdesc.setHost(properties.getProperty("host"));
		dbdesc.setPort(properties.getProperty("port"));
		dbdesc.setDbname(properties.getProperty("dbname"));
		dbdesc.setUsername(properties.getProperty("username"));
		dbdesc.setPassword(properties.getProperty("password"));
	}
	
	synchronized Connection initConnection(){
		if(conn != null)
			return conn;
		try {
			String dburl = null;
			String driverName = null;
			String port = dbdesc.getPort();	
			switch(dbdesc.getDb()){
				case MSSQLSERVER:			
					port = port==null?"1433":port;
					dburl = "jdbc:sqlserver://"+dbdesc.getHost()+":"+port+";DatabaseName="+dbdesc.getDbname();
					driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
					break;
				case MYSQL:
					port = port==null?"3306":port;
					dburl = "jdbc:mysql://"+dbdesc.getHost()+":"+port+"/"+dbdesc.getDbname();
					driverName = "com.mysql.jdbc.Driver";
					break;
				case ORACLE:	
					port = port==null?"1521":port;
					dburl = "jdbc:oracle:thin:@"+dbdesc.getHost()+":"+port+":"+dbdesc.getDbname();
					driverName = "oracle.jdbc.driver.OracleDriver"; 
					break;					
			}
			if(dburl == null || driverName == null)
				throw new Exception("unknown DB type : " + dbdesc.getDb().toString());
			
			Class.forName(driverName).newInstance();
			conn = DriverManager.getConnection(dburl, dbdesc.getUsername(), dbdesc.getPassword());
						
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return conn;
	}
	
	

	@Override
	public Connection getConnection() throws SQLException {
		if(conn == null)
			initConnection();
		return conn;
	}

	@Override
	public Connection getConnection(String username, String password)
			throws SQLException {
		if(conn == null){
			if(dbdesc != null){
				dbdesc.setUsername(username);
				dbdesc.setPassword(password);
				initConnection();
			}else 
				throw new SQLException("db has not been configuire");
		}	
		return conn;
	}

	
	
	
	@Override
	public PrintWriter getLogWriter() throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int getLoginTimeout() throws SQLException {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public void setLogWriter(PrintWriter out) throws SQLException {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void setLoginTimeout(int seconds) throws SQLException {
		// TODO Auto-generated method stub
		
	}

	public boolean isWrapperFor(Class<?> iface) throws SQLException {
		// TODO Auto-generated method stub
		return false;
	}

	public <T> T unwrap(Class<T> iface) throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	
	public DBDesc getDbcfg() {
		return dbdesc;
	}

	public void setDbcfg(DBDesc dbcfg) {
		this.dbdesc = dbcfg;
	}

	
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}
	
}
